import request from "../utils/request";

export const login = async (username: string, password: string) => {
    return await request.post("/user/login", {
        loginAccount: username,
        loginPassword: password
    });
}

export const getCurrentUser = async () => {
    const res = await request.get("/user/current");
    if (res.data.code === 200) {
        return res.data.data;
    }
}

export const updateUser = async (id: number, editKey: string, value: string | number) => {
    return await request.post("/user/update", {
        id: id,
        [editKey]: value
    });
}

export const searchByTags = async (tags: any) => {
    const res = await request.post("/user/searchByTags", {
        tagNameList: tags
    });
    if (res.data.code === 200) {
        const userList = res.data.data;
        // 将标签转换成数组
        userList.forEach((user: any) => {
            user.tags = JSON.parse(user.tags)
        })
        return userList;
    }
}

export const recommend = async (tags: any) => {
    const res = await request.get("/user/recommend", {
        params: {
            pageNum: 1,
            pageSize: 8
        }
    });
    if (res.data.code === 200) {
        const userList = res.data.data.records;
        // 将标签转换成数组
        userList.forEach((user: any) => {
            user.tags = JSON.parse(user.tags)
        })
        return userList;
    }
}